package com.bbva.kyfb.kyfbjpatlsb.dominiotlsb;

import java.math.BigDecimal;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.dod.RooDataOnDemand;
import org.springframework.stereotype.Component;

/**
 * The Class TtlsbffsDataOnDemand.
 */
@Configurable
@Component
@RooDataOnDemand(entity = Ttlsbffs.class)
public class TtlsbffsDataOnDemand {

	/** The rnd. */
	private Random rnd = new SecureRandom();

	/** The data. */
	private List<Ttlsbffs> data;

	/**
	 * Gets the new transient ttlsbffs.
	 *
	 * @param index the index
	 * @return the new transient ttlsbffs
	 */
	public Ttlsbffs getNewTransientTtlsbffs(int index) {
        Ttlsbffs obj = new Ttlsbffs();
        setEmbeddedIdClass(obj, index);
        setAudFmodific(obj, index);
        setAudUsuario(obj, index);
        setCodBancoint(obj, index);
        setCodBancopr(obj, index);
        setCodCanaldi(obj, index);
        setCodLogon2(obj, index);
        setCodNumaleat(obj, index);
        setCodProdserv(obj, index);
        setCodSubprodu(obj, index);
        setDesNomfiche(obj, index);
        setXsnFirmado(obj, index);
        setXtiPodfir(obj, index);
        setXtiTipoapod(obj, index);
        return obj;
    }

	/**
	 * Sets the embedded id class.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setEmbeddedIdClass(Ttlsbffs obj, int index) {
		TtlsbffsPK embeddedIdClass;
		if(String.valueOf(index).length() > 1){
			embeddedIdClass = new TtlsbffsPK(BigDecimal.valueOf(index), new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime(), "codUsu_" + String.valueOf(index).substring(0,1));
		}else{
			embeddedIdClass = new TtlsbffsPK(BigDecimal.valueOf(index), new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime(), "codUsu_" + index);
		}
        obj.setId(embeddedIdClass);
    }

	/**
	 * Sets the aud fmodific.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudFmodific(Ttlsbffs obj, int index) {
        Date audFmodific = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setAudFmodific(audFmodific);
    }

	/**
	 * Sets the aud usuario.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setAudUsuario(Ttlsbffs obj, int index) {
        String audUsuario = "audUsu_" + index;
        if (audUsuario.length() > 8) {
            audUsuario = audUsuario.substring(0, 8);
        }
        obj.setAudUsuario(audUsuario);
    }

	/**
	 * Sets the cod bancoint.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodBancoint(Ttlsbffs obj, int index) {
        Short codBancoint = new Integer(index).shortValue();
        obj.setCodBancoint(codBancoint);
    }

	/**
	 * Sets the cod bancopr.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodBancopr(Ttlsbffs obj, int index) {
        Short codBancopr = new Integer(index).shortValue();
        obj.setCodBancopr(codBancopr);
    }

	/**
	 * Sets the cod canaldi.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodCanaldi(Ttlsbffs obj, int index) {
        Short codCanaldi = new Integer(index).shortValue();
        obj.setCodCanaldi(codCanaldi);
    }

	/**
	 * Sets the cod logon2.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodLogon2(Ttlsbffs obj, int index) {
        String codLogon2 = "codLogon2_" + index;
        if (codLogon2.length() > 20) {
            codLogon2 = codLogon2.substring(0, 20);
        }
        obj.setCodLogon2(codLogon2);
    }

	/**
	 * Sets the cod numaleat.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodNumaleat(Ttlsbffs obj, int index) {
        String codNumaleat = "codNuma_" + index;
        if (codNumaleat.length() > 9) {
            codNumaleat = codNumaleat.substring(0, 9);
        }
        obj.setCodNumaleat(codNumaleat);
    }

	/**
	 * Sets the cod prodserv.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodProdserv(Ttlsbffs obj, int index) {
        Short codProdserv = new Integer(index).shortValue();
        obj.setCodProdserv(codProdserv);
    }

	/**
	 * Sets the cod subprodu.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setCodSubprodu(Ttlsbffs obj, int index) {
        Short codSubprodu = new Integer(index).shortValue();
        obj.setCodSubprodu(codSubprodu);
    }

	/**
	 * Sets the des nomfiche.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setDesNomfiche(Ttlsbffs obj, int index) {
        String desNomfiche = "desNomfiche_" + index;
        if (desNomfiche.length() > 40) {
            desNomfiche = desNomfiche.substring(0, 40);
        }
        obj.setDesNomfiche(desNomfiche);
    }

	/**
	 * Sets the xsn firmado.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXsnFirmado(Ttlsbffs obj, int index) {
        Character xsnFirmado = new Character('N');
        obj.setXsnFirmado(xsnFirmado);
    }

	/**
	 * Sets the xti podfir.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXtiPodfir(Ttlsbffs obj, int index) {
        String xtiPodfir = "x" + index;
        if (xtiPodfir.length() > 2) {
            xtiPodfir = xtiPodfir.substring(0, 2);
        }
        obj.setXtiPodfir(xtiPodfir);
    }

	/**
	 * Sets the xti tipoapod.
	 *
	 * @param obj the obj
	 * @param index the index
	 */
	public void setXtiTipoapod(Ttlsbffs obj, int index) {
        Character xtiTipoapod = new Character('N');
        obj.setXtiTipoapod(xtiTipoapod);
    }

	/**
	 * Gets the specific ttlsbffs.
	 *
	 * @param index the index
	 * @return the specific ttlsbffs
	 */
	public Ttlsbffs getSpecificTtlsbffs(int index) {
        init();
        if (index < 0) index = 0;
        if (index > (data.size() - 1)) index = data.size() - 1;
        Ttlsbffs obj = data.get(index);
        return Ttlsbffs.findTtlsbffs(obj.getId());
    }

	/**
	 * Gets the random ttlsbffs.
	 *
	 * @return the random ttlsbffs
	 */
	public Ttlsbffs getRandomTtlsbffs() {
        init();
        Ttlsbffs obj = data.get(rnd.nextInt(data.size()));
        return Ttlsbffs.findTtlsbffs(obj.getId());
    }

	/**
	 * Modify ttlsbffs.
	 *
	 * @param obj the obj
	 * @return true, if successful
	 */
	public boolean modifyTtlsbffs(Ttlsbffs obj) {
        return false;
    }

	/**
	 * Inits the.
	 */
	public void init() {
        data = Ttlsbffs.findTtlsbffsEntries(0, 10);
        if (data == null) throw new IllegalStateException("Find entries implementation for 'Ttlsbffs' illegally returned null");
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<com.bbva.kyfb.kyfbjpatlsb.dominiotlsb.Ttlsbffs>();
        for (int i = 0; i < 10; i++) {
            Ttlsbffs obj = getNewTransientTtlsbffs(i);
            try {
                obj.persist();
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> it = e.getConstraintViolations().iterator(); it.hasNext();) {
                    ConstraintViolation<?> cv = it.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            obj.flush();
            data.add(obj);
        }
    }
}
